const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const config = require('../config')

const User = mongoose.model('User')

const checkToken = async(token) => {
    if(token){
        // console.log(token);
        var decoded = jwt.verify(token,config.secretKey)
        return await User.find({username:decoded.username,password:decoded.password},'username password')
        .then(([user]) => {
            // console.log(user?{isLogin:true,username:decoded.username}:{isLogin:false});
            return user?{isLogin:true,username:decoded.username}:{isLogin:false};
        })
    }else{
        return {isLogin:false};
    }
}
module.exports = checkToken;
